#region Microsoft Public License
/*
 * (c) Copyright Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using UTable.Input;
using UTable.Input.MultiTouch;
using UTable.Objects;
using UTable.Objects.Controls;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Windows.Media;

namespace UTable.Objects.Controls
{
    /// <summary>
    /// Interaction logic for UBookControl.xaml
    /// </summary>
    public partial class UBookControl : UUserControl, IControlDisposable
    {
        public UBookControl()
        {
            InitializeComponent();

            this.DataContext = this;
			PagePathList = new ObservableCollection<UBookControlPage>();
            DependencyPropertyDescriptor.FromProperty(UBookControl.PagesFolderPathProperty,
                    typeof(UBookControl)).AddValueChanged(this, new EventHandler(OnPagesFolderPathChanged));
			this.Loaded += new RoutedEventHandler(UBookControl_Loaded);
            
        }

        #region Private Methods

        void UBookControl_Loaded(object sender, RoutedEventArgs e)
		{
            OnWorkTypeChanged();
		}

		private void OnPagesFolderPathChanged(object sender, EventArgs args)
        {
            PagePathList.Clear();
            String[] files = Directory.GetFiles(PagesFolderPath);
            List<String> fileList = new List<string>(files);
            fileList.Sort();
            foreach (String file in fileList)
            {
                if (File.Exists(file))
                {
					UBookControlPage page = new UBookControlPage();
					page.PageSource = file;
                    PagePathList.Add(page);
                }
            }
		}

		private UPaintCanvas GetLeftInkCanvas()
		{
			int pageIndex = BookViewerControl.CurrentSheetIndex * 2 - 1;
			if (pageIndex < 0 || pageIndex >= BookViewerControl.GetItemsCount())
				return null;
			UBookControlPage page = BookViewerControl.GetPage(pageIndex) as UBookControlPage;
			return page.InkCanvas;
		}

        private UPaintCanvas GetRightInkCanvas()
		{
			int pageIndex = BookViewerControl.CurrentSheetIndex * 2;
			if (pageIndex < 0 || pageIndex >= BookViewerControl.GetItemsCount())
				return null;
			UBookControlPage page = BookViewerControl.GetPage(pageIndex) as UBookControlPage;
			return page.InkCanvas;
		}

        private static void OnWorkTypeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs pty)
		{
            (obj as UBookControl).OnWorkTypeChanged();
		}

        private void OnWorkTypeChanged()
		{
            switch (this.WorkType)
            {
                case UBookWorkType.Reading:
                    foreach (UBookControlPage page in this.BookViewerControl.Items)
                    {
                        page.InkCanvas.EditingMode = InkCanvasEditingMode.None;
                    }
                    break;
                case UBookWorkType.Noting:
                    foreach (UBookControlPage page in this.BookViewerControl.Items)
                    {
                        page.InkCanvas.EditingMode = InkCanvasEditingMode.Ink;
                    }
                    break;
                case UBookWorkType.Erasering:
                    foreach (UBookControlPage page in this.BookViewerControl.Items)
                    {
                        page.InkCanvas.EditingMode = InkCanvasEditingMode.EraseByStroke;
                    }
                    break;
            }
		}

		#endregion

		#region Properties

		public static readonly DependencyProperty PagesFolderPathProperty =
			DependencyProperty.Register("PagesFolderPath", typeof(String), typeof(UBookControl), new UIPropertyMetadata(string.Empty));

		public String PagesFolderPath
		{
			get { return (String)GetValue(PagesFolderPathProperty); }
			set { SetValue(PagesFolderPathProperty, value); }
		}

		public ObservableCollection<UBookControlPage> PagePathList { get; set; }


        public UBookWorkType WorkType
        {
            get { return (UBookWorkType)GetValue(WorkTypeProperty); }
            set { SetValue(WorkTypeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for WorkType.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty WorkTypeProperty =
            DependencyProperty.Register("WorkType", typeof(UBookWorkType), typeof(UBookControl), new UIPropertyMetadata(UBookWorkType.Reading, new PropertyChangedCallback(OnWorkTypeChanged)));        

		#endregion

		#region Public Methods

        public void DisposeControl()
        {
            DependencyPropertyDescriptor.FromProperty(UBookControl.PagesFolderPathProperty,
                    typeof(UBookControl)).RemoveValueChanged(this, new EventHandler(OnPagesFolderPathChanged));
        }

		public void ClearLeftPageNote()
		{
			UPaintCanvas inkCanvas = GetLeftInkCanvas();
			if (inkCanvas == null)
				return;
			inkCanvas.Children.Clear();
			inkCanvas.Strokes.Clear();
		}

		public void ClearRightPageNote()
		{
            UPaintCanvas inkCanvas = GetRightInkCanvas();
			if (inkCanvas == null)
				return;
			inkCanvas.Children.Clear();
			inkCanvas.Strokes.Clear();
		}

		#endregion
	}

    public enum UBookWorkType
    {
        Reading,
        Noting,
        Erasering,
    }
}
